package com.outstanding.junit.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.outstanding.framework.core.MultiPendingException;
import com.outstanding.framework.core.OutStandingException;
import com.outstanding.framework.core.PendingException;
import com.outstanding.junit.code.ResCode;
import com.outstanding.junit.constant.SessionConstant;
import com.outstanding.junit.dto.voice.VoiceReqDto;
import com.outstanding.junit.dto.voice.VoiceResDto;
import com.outstanding.junit.service.VoiceService;
import com.outstanding.junit.utils.SystemUtils;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Controller
@RequestMapping("/message")
public class MessageController {
	
	@Autowired
	private VoiceService voiceService;

	@RequestMapping(value = "/sendVoiceCode.do")
	public @ResponseBody VoiceResDto sendVoiceCode(@RequestBody VoiceReqDto dto,HttpSession session) throws OutStandingException {
		
		VoiceResDto res = new VoiceResDto();
		
		String auth = (String) session.getAttribute(SessionConstant.USER_LOGIN_AUTH);
		
		SystemUtils.outPrint("session值", auth);
		
		if (!auth.equals("true")) {
			throw new PendingException(ResCode.userNotLogin.getCode(), ResCode.userNotLogin.getName());
		}
		
		if (!dto.validate()) {
			throw new PendingException(ResCode.paramError.getCode(), ResCode.paramError.getName());
        }
		
		if (!voiceService.sendVoice(dto)) {
			throw new PendingException(ResCode.voiceSendFail.getCode(), ResCode.voiceSendFail.getName());
		}
		
		res.setRspCd(ResCode.success.getCode());
		
		return res;
	}
}
